﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SRF.Data.SQLParser
{
    /// <summary>
    ///  sql解析器工厂
    /// </summary>
    public static class SQLParserFactory
    {
        static IDictionary<string, ISQLParser> sqlParsers;

        static SQLParserFactory()
        {
            sqlParsers = new Dictionary<string, ISQLParser>();
            sqlParsers.Add("velocity", VelocitySQLParser.Get());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static ISQLParser Get(string mode)
        {
            if (string.IsNullOrEmpty(mode))
            {
                return DefaultSQLParser.Get();
            }
            else
            {
                if (sqlParsers.ContainsKey(mode.ToLower()))
                {
                    return sqlParsers[mode.ToLower()];
                }
                else
                {
                    throw new KeyNotFoundException("SQLParser不存在");
                }
            }
        }

        /// <summary>
        /// 注册sql解析器
        /// </summary>
        /// <param name="key">名称</param>
        /// <param name="sqlParser">sql解析器</param>
        public static void Register(string key, ISQLParser sqlParser)
        {
            key = key.ToLower();
            if (sqlParsers.ContainsKey(key))
            {
                sqlParsers[key] = sqlParser;
            }
            else
            {
                sqlParsers.Add(key, sqlParser);
            }
        }
    }
}